﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QuanLyPhongKham
{
    public partial class ucPhanQuyenSuDung : BaseUserControl
    {
        private DAL.AccessData m;
        private int i_id;
        private DataTable dtpk = new DataTable();
        private DataTable dtct = new DataTable();
        private string sql = "";
        public ucPhanQuyenSuDung(DAL.AccessData acc)
        {
            this.m = acc;
            InitializeComponent();
            this.Title = "Phân quyền";

        }

        private void frmPhanquyensudung_Load(object sender, EventArgs e)
        {
            i_id = 0;
            dtpk = m.get_data("select makp,tenkp from " + m.user + ".btdkp order by tenkp").Tables[0];
            phongkham.DataSource = dtpk;
            phongkham.ValueMember = "MAKP";
            phongkham.DisplayMember = "TENKP";
            load_grid();
        }
        private void load_grid()
        {
            sql = "select * from "+m.user+".dlogin order by userid";
            dtct = m.get_data(sql).Tables[0];
            this.gridControl1.DataSource = dtct;
        }
        
        private void reft_text()
        {
            //try
            //{
            //    int i = dataGrid1.CurrentCell.RowNumber;
            //    i_id = int.Parse(dataGrid1[i, 4].ToString());
            //    userid.Text = dataGrid1[i, 0].ToString();
            //    hoten.Text = dataGrid1[i, 1].ToString();
            //    pass.Text = dataGrid1[i, 5].ToString();
            //    foreach (CheckBox chk in gSudung.Controls)
            //    {
            //        if (dataGrid1[i, 2].ToString().IndexOf(chk.Name) != -1)
            //        {
            //            chk.Checked = true;
            //        }
            //        else chk.Checked = false;
            //    }
            //    for (int j = 0; j < phongkham.Items.Count; j++)
            //    {
            //        if (dataGrid1[i, 3].ToString().IndexOf(dtpk.Rows[j]["makp"].ToString() + ",") != -1)
            //        {
            //            phongkham.SetItemCheckState(j,CheckState.Checked);
            //        }
            //        else phongkham.SetItemCheckState(j, CheckState.Unchecked);
            //    }
            //    hoten.Enabled = false; userid.Enabled = false; pass.Enabled = true;
            //}
            //catch { }
        }
        public Color MyGetColorRowCol(int row, int col)
        {
            Color c = Color.Navy;
            //try
            //{
            //    if (row == dataGrid1.CurrentRowIndex)
            //    {
            //        dataGrid1.Select(row);
            //        c = Color.White;
            //    }
            //}
            //catch
            //{
            //}
            return c;
        }
        public delegate Color delegateGetColorRowCol(int row, int col);
        public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
        {
            private delegateGetColorRowCol _getColorRowCol;
            private int _column;
            public DataGridColoredTextBoxColumn(delegateGetColorRowCol getcolorRowCol, int column)
            {
                _getColorRowCol = getcolorRowCol;
                _column = column;
            }
            protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
            {
                try
                {
                    foreBrush = new SolidBrush(_getColorRowCol(rowNum, this._column));
                }
                catch { }
                finally
                {
                    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
                }
            }
        }

        private void butMoi_Click(object sender, EventArgs e)
        {
            i_id = 0;
            empty_detail();
            ena_object(true);
            gSudung.Enabled = true; phongkham.Enabled = true;
            hoten.Focus();
        }
        private void ena_object(bool ena)
        {
            hoten.Enabled = ena;
            userid.Enabled = ena;
            pass.Enabled = ena;
            foreach (CheckBox ctr in gSudung.Controls)
            {
                ctr.Checked = false;
            }
        }
        private void empty_detail()
        {
            hoten.Text = "";
            userid.Text = "";
            pass.Text = "";
        }
        private void hoten_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
            }
        }

        private void butLuu_Click(object sender, EventArgs e)
        {
            if (hoten.Text.Trim() == "")
            {
                hoten.Focus(); return;
            }
            if (userid.Text.Trim() == "")
            {
                userid.Focus(); return;
            }
            if (pass.Text.Trim() == "")
            {
                pass.Focus(); return;
            }
            if (get_menu() == "")
            {
                phongkham.Focus(); return;
            }
            if (get_phongkham() == "")
            {
                phongkham.Focus(); return;
            }
            i_id = (i_id == 0) ? m.get_capid_dlogin : i_id;
            m.upd_dlogin(i_id, hoten.Text, userid.Text, pass.Text, get_menu(), get_phongkham(), 0, 0);
            load_grid();
            reft_text();
            ena_object(false);
        }
        private string get_menu()
        {
            string _menu = "";
            foreach (CheckBox chk in gSudung.Controls)
            {
                if (chk.Checked)
                {
                    _menu += chk.Name + ",";
                }
            }
            return _menu;
        }
        private string get_phongkham()
        {
            string _makp = "";
            for (int i = 0; i < phongkham.Items.Count; i++)
            {
                if (phongkham.GetItemChecked(i))
                {
                    _makp += dtpk.Rows[i]["makp"].ToString()+",";
                }
            }
            return _makp;
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            reft_text();
        }

        private void butKetthuc_Click(object sender, EventArgs e)
        {
            //fix:
            //this.Close();
        }

        private void butHuy_Click(object sender, EventArgs e)
        {
            if (i_id == 0) return;
            if (MessageBox.Show("Bạn có đồng ý huỷ thông tin này không?", DAL.AccessData.Msg, MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                m.execute_data("delete from " + m.user + ".dlogin where id=" + i_id);
                load_grid(); reft_text();
            }
        }
    }
}